fastlane_version "2.68.0"

$LOAD_PATH << '../lib'
require 'milkrun'
require_relative '../config/initializer'

default_platform :android

lane :alpha do
  audience      = 'internal'
  build_type    = 'debug'
  version_code  = Milkrun::VersionCode.new(audience: audience).bump
  version_name  = Milkrun::VersionName.new(audience: audience).read
  slack_message = ["🚀",audience,build_type,"|",version_name,version_code,"successfully delivered to Firebase!"].join(' ')

  build_android_app(task: "assembleInternalDebug")

  firebase_app_distribution(
    app: Milkrun::FirebaseCredentialsExtractor.new.internal_debug,
    groups: "android-testers, dev",
    release_notes: commit_message
  )

  slack(
    slack_url: Milkrun::SlackWebhookExtractor.new.webhook,
    message: slack_message
  )
end

lane :external do
  audience      = 'external'
  build_type    = 'release'
  version_code  = Milkrun::VersionCode.new(audience: audience).bump
  version_name  = Milkrun::VersionName.new(audience: audience).read
  slack_message = ["🚀",audience,build_type,"|",version_name,version_code,"successfully delivered to Firebase!"].join(' ')
  release_notes = changelog_from_git_commits(pretty: "- %an | %s %h | %as")

  build_android_app(task: "assembleExternalRelease")

  firebase_app_distribution(
    app: Milkrun::FirebaseCredentialsExtractor.new.external_release,
    groups: "android-testers, dev",
    release_notes: release_notes
  )

  slack(
    slack_url: Milkrun::SlackWebhookExtractor.new.webhook,
    message: slack_message
  )
end

lane :internal do
  audience      = 'internal'
  build_type    = 'release'
  version_code  = Milkrun::VersionCode.new(audience: audience).bump
  version_name  = Milkrun::VersionName.new(audience: audience).read
  slack_message = ["🚀",audience,build_type,"|",version_name,version_code,"successfully delivered to S3 and Firebase!"].join(' ')

  build_android_app(task: "assembleInternalRelease")

  path_to_apk = lane_context[SharedValues::GRADLE_APK_OUTPUT_PATH]
  Milkrun::S3Package.new(version_code: version_code, file_path: path_to_apk).upload

  changelog = Milkrun::Changelog.new(audience: audience, build_type: build_type, version_code: version_code)
  commit_message = last_git_commit[:message]
  changelog.instance_variable_set(:@changelog, commit_message)
  changelog.publish

  firebase_app_distribution(
    app: Milkrun::FirebaseCredentialsExtractor.new.internal_release,
    groups: "android-testers, dev",
    release_notes: commit_message
  )

  slack(
    slack_url: Milkrun::SlackWebhookExtractor.new.webhook,
    message: slack_message
  )

end
